package org.wattdepot.cli.commands;

import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;

/**
 * This class tests the power sampling command of the 
 * CommandLineInterface's actual commands.
 * @author David Mau
 */
public class TestCommandPowerSampling {
  String cr = System.getProperty("line.separator");

  /**
   * Test normal operation of the power samplings command.
   */
  @Test
  public void testPowerSamplings() {
    // Create a new power sampling command object.
    PowerSamplingCommand command = new PowerSamplingCommand(null);
    // Create a new List to store the args
    List<String> args = Arrays.asList( "SIM_WAIAU_8", "2009-11-15", "60", "average" );
    
    // Get the output from the command
    String sourceOutput = command.doCommand( args );
    
    // Ready the output that we know is true
    String knownOutput = "71.58333333333333 Megawatts";
    knownOutput = knownOutput.concat(cr);
    
    // Compare the two to see if they are the same.
    int result = sourceOutput.compareTo(knownOutput);
    assertSame("Checking known average at Waiau #8 grid with good timestamp", result, 0);
    
    // Create a new List to store the args
    args = Arrays.asList( "SIM_WAIAU", "2009-11-15", "60", "min" );
    
    // Get the output from the command
    sourceOutput = command.doCommand( args );

    // Ready the output that we know is true
    knownOutput = "116.0 Megawatts";
    knownOutput = knownOutput.concat(cr);
    
    // Compare the two to see if they are the same.
    result = sourceOutput.compareTo(knownOutput);
    assertSame("Checking known minimum at Waiau's with good timestamp", result, 0);
  }

  /**
   * Test bad time stamp detection for the power sampling command.
   */
  @Test
  public void testPowerSamplingsBadTime() {
    // Create a new power sampling command object.
    PowerSamplingCommand command = new PowerSamplingCommand(null);
    // Create a new List to store the args
    List<String> args = Arrays.asList( "SIM_WAIAU", "HAHAHA", "10", "min" );
    
    // Get the output from the command
    String sourceOutput = command.doCommand( args );
    
    // Check to see if the correct error returned
    assertTrue("Checking for correct response to a bad timestamp", sourceOutput
        .contains(WattDepotCliCommand.badTimeStamp));

  }

  
  /**
   * Test bad source detection for the power ampling command.
   */
  @Test
  public void testPowerSamplingBadSource() {
    // Create a new power sampling command object.
    PowerSamplingCommand command = new PowerSamplingCommand(null);
    
    // Create a new List to store the args
    List<String> args = Arrays.asList( "GHOSTS", "2009-11-03", "35", "max" );
    
    // Get the output from the command
    String sourceOutput = command.doCommand( args );
    
    // Check to see if the correct error returned
    assertTrue("Checking for correct response to a bad source", 
        sourceOutput.contains(WattDepotCliCommand.resourceNotFound));
  }

  /**
   * Test bad option given for statistic for the power sampling command.
   */
  @Test
  public void testPowerSamplingBadStatistic() {
    // Create a new power sampling command object.
    PowerSamplingCommand command = new PowerSamplingCommand(null);
    // Create a new List to store the args
    List<String> args = Arrays.asList( "SIM_HONOLULU", "2009-11-01", "120", "EAT_AT_JOES" );
    
    // Get the output from the command
    String sourceOutput = command.doCommand( args );
    
    // Check to see if the correct error returned
    assertTrue("Checking for correct response to a bad statistic", 
        sourceOutput.contains(WattDepotCliCommand.minOrMaxOrAverage));
    
  }


  /**
   * Test a bad interval given for the power sampling command. The given String should be
   * convertible into an Integer.
   */
  @Test
  public void testPowerSamplingBadInterval() {
    // Attempt to use a bad interval
    // Create a new power sampling command object.
    PowerSamplingCommand command = new PowerSamplingCommand(null);
    // Create a new List to store the args
    List<String> args = Arrays.asList( "SIM_KAHE_1", "2009-11-02", "BATS", "max" );
    
    // Get the output from the command
    String sourceOutput = command.doCommand( args );
    
    // Check to see if the correct error returned
    assertTrue("Checking for correct response to a bad interval", sourceOutput
        .contains(WattDepotCliCommand.intervalNotNumber));
    
  }

  /**
   * Test another bad interval given for the power sampling command.
   * The given String should be a non-zero, positive integer.
   */
  @Test
  public void testPowerSamplingBadIntervalInteger() {
    // Create a new power sampling command object.
    PowerSamplingCommand command = new PowerSamplingCommand(null);
    
    // Attempt to use a negative interval
    // Create a new List to store the args
    List<String> args = Arrays.asList( "SIM_KAHE_3", "2009-11-04", "-1", "average" );
    
    // Get the output from the command
    String sourceOutput = command.doCommand( args );
    
    // Check to see if the correct error returned
    assertTrue("Checking for correct response to a negative interval", sourceOutput
        .contains(WattDepotCliCommand.intervalNegativeOrZero));
    
    // Attempt to use zero interval
    // Create a new List to store the args
    args = Arrays.asList( "SIM_AES", "2009-11-05", "0", "min" );
    
    // Get the output from the command
    sourceOutput = command.doCommand( args );
    
    // Check to see if the correct error returned
    assertTrue("Checking for correct response to a zero interval", sourceOutput
        .contains(WattDepotCliCommand.intervalNegativeOrZero));
  }  

  /**
   * Tests to see if the power sampling command can handle excess arguments.
   */
  @Test
  public void testExcessArguments() {
    // Create a new power sampling command object.
    PowerSamplingCommand command = new PowerSamplingCommand(null);
    // Create a new List to store the args
    List<String> args = Arrays.asList("1", "2", "3", "4", "5");
    
    // Get the output from the command
    String sourceOutput = command.doCommand( args );

    // Make sure that the command is returning the error properly
    assertTrue("Checking excess Power Sampling argument handling", 
        sourceOutput.contains(WattDepotCliCommand.tooManyArguments));
  }
  
  /**
   * Tests to see if the power sampling command can handle not enough arguments.
   */
  @Test
  public void testFewArguments() {
    // Create a new power sampling command object.
    PowerSamplingCommand command = new PowerSamplingCommand(null);
    // Create a new List to store the args
    List<String> args = Arrays.asList("1");
    
    // Get the output from the command
    String sourceOutput = command.doCommand( args );

    // Make sure that the command is returning the error properly
    assertTrue("Checking Power Sampling handling not enough arguments", 
        sourceOutput.contains(WattDepotCliCommand.missingArguments));
  }
}
